<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root"></div>
    <script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <script type="text/babel">
        class InputElement extends React.Component{
            constructor(){
                super();
            
            }
          
            render(){
                return <div>
                        <label>{this.props.children}</label>
                        <input type={this.props.type} onBlur={this.props.handleValidate}
                            onInput={this.props.onInput}
                            value={this.props.value}
                        />
                        <label>{this.props.info}</label>
                    </div>
            }
        }
        class Reg extends React.Component{
            constructor(){
                super();
                this.state = {
                    username:{
                        value:"",
                        info:""
                    },
                    pwd:{
                        value:"",
                        info:""
                    }
                }
            }
            validateUsername = (e) => {
                if(/^\w{6,20}$/.test(e.target.value)){
                    this.setState({
                        username:{
                            value:this.state.username.value,
                            info:"√"
                        }
                    });
                }else{
                    this.setState({
                        username:{
                            value:this.state.username.value,
                            info:"X"
                        }
                    });
                }
            }
            handleSubmit = ()=>{
                console.log(this.state.username.value);
            }

            render(){
                return <div>
                    <h1>注册</h1>
                    <InputElement handleValidate={this.validateUsername} 
                    info={this.state.username.info}
                    value={this.state.username.value}
                    onInput={(e) => this.setState({username:{value:e.target.value}})}
                    >用户名：</InputElement>
                    <InputElement type="password">密码：</InputElement>
                    <InputElement type="password">确认密码：</InputElement>
                    <input type="button" value="提交" onClick={this.handleSubmit}/>
                </div>
            }
        }
        ReactDOM.render(<Reg />,document.getElementById("root"));
    </script>
</body>
</html>